package link_docker

import (
	"github.com/docker/docker/client"
	"gopkg.in/yaml.v3"
	"os"
)

type conf struct {
	Docker struct {
		Host string `yaml:"host"`
		Port string `yaml:"port"`
	}
}

var ConfigData *conf

func loadConfig() error {
	config := new(conf)
	yamlFlie, err := os.ReadFile("E:\\bridge\\config\\config.yml")
	if err != nil {
		panic(err)
	}
	err = yaml.Unmarshal(yamlFlie, config)
	if err != nil {
		panic(err)
	}
	ConfigData = config
	return nil
}

func Client() *client.Client {
	err := loadConfig()
	if err != nil {
		panic(err)
	}
	cli, err := client.NewClientWithOpts(client.WithHost("tcp://"+ConfigData.Docker.Host+":"+ConfigData.Docker.Port), client.WithAPIVersionNegotiation())
	if err != nil {
		panic(err)
	}
	return cli
}
